package hust.iamk.lock;

import java.io.File;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.OnScanCompletedListener;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;
import android.util.Log;

public class ListFileUtil {

	public ArrayList<String> listFiles(String directoryName,String extensionFilter) {
		ArrayList<String> arrFilePath = new ArrayList<String>();
		File directory = new File(directoryName);

		// get all the files from a directory
		File[] fList = directory.listFiles();

		for (File file : fList) {
			if (file.isFile() && file.getName().toLowerCase().endsWith(extensionFilter)) {
				arrFilePath.add(file.getAbsolutePath());
			}
		}
		return arrFilePath;
	}
	
	public ArrayList<String> listFiles(String directoryName) {
		ArrayList<String> arrFilePath = new ArrayList<String>();
		File directory = new File(directoryName);

		// get all the files from a directory
		File[] fList = directory.listFiles();
		if(fList !=  null){
			for (File file : fList) {
				if (file.isFile()) {
					arrFilePath.add(file.getAbsolutePath());
				}
			}
		}
		return arrFilePath;
	}
	
	public String[] listFilesArray(String directoryName) {
		String[] arrFilePath = null;
		File directory = new File(directoryName);

		// get all the files from a directory
		File[] fList = directory.listFiles();
		if(fList !=  null){
			arrFilePath = new String[fList.length];
			for (int i = 0; i < fList.length; i++) {
				if (fList[i].isFile()) {
					arrFilePath[i] = fList[i].getAbsolutePath();
				}
			}
		}
		return arrFilePath;
	}
	
	public void deleteFiles(String directoryName,String extensionFilter, Context mContext) {
		File directory = new File(directoryName);

		// get all the files from a directory
		File[] fList = directory.listFiles();
		if(fList != null){
			for (int i = 0; i < fList.length; i++) {
				if (fList[i].isFile() && fList[i].getName().toLowerCase().endsWith(extensionFilter)) {
					Log.d("delete", SecurityFileUtils.getFileName(fList[i].getAbsolutePath(), 0));
					fList[i].delete();
					if(extensionFilter.equals(".jpg") || extensionFilter.equals(".png")){
						ContentResolver resolver = mContext.getContentResolver();
						resolver.delete(Images.Media.EXTERNAL_CONTENT_URI,  Images.Media.DATA +  " = ? ", new String[]{fList[i].getAbsolutePath()});
					}else{
						ContentResolver resolver = mContext.getContentResolver();
						resolver.delete(Video.Media.EXTERNAL_CONTENT_URI,  Video.Media.DATA +  " = ? ", new String[]{fList[i].getAbsolutePath()});
					}
					  
				}
			}
		}
		
	}
}
